home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / VISCAFE.BIN / FtpClient.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-06-19  |  5.6 KB  |  266 lines

  1. package symantec.itools.db.awt;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.IOException;
  5. import java.io.PrintStream;
  6. import java.net.InetAddress;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9. import java.net.UnknownHostException;
  10. import java.util.StringTokenizer;
  11. import java.util.Vector;
  12.  
  13. public class FtpClient {
  14.    String host;
  15.    // $FF: renamed from: s java.net.Socket
  16.    Socket field_0;
  17.    boolean Connected = false;
  18.    boolean LoggedIn = false;
  19.    PrintStream ftpout;
  20.    DataInputStream ftpin;
  21.    FtpErrors errs;
  22.    // $FF: renamed from: v java.util.Vector
  23.    Vector field_1;
  24.    Vector files;
  25.    Vector dirs;
  26.    private boolean dataRead = false;
  27.    public static final int ASCII = 0;
  28.    public static final int BINARY = 1;
  29.    private int xferMode = 1;
  30.    private boolean modeChange = true;
  31.  
  32.    public FtpClient(String var1) throws IOException {
  33.       this.host = var1;
  34.       this.Connected = true;
  35.       this.errs = new FtpErrors();
  36.  
  37.       try {
  38.          this.field_0 = new Socket(var1, 21);
  39.       } catch (UnknownHostException var6) {
  40.          this.Connected = false;
  41.       }
  42.  
  43.       this.ftpout = new PrintStream(this.field_0.getOutputStream());
  44.       this.ftpin = new DataInputStream(this.field_0.getInputStream());
  45.       String var2 = this.ftpin.readLine();
  46.  
  47.       int var3;
  48.       try {
  49.          var3 = Integer.parseInt(var2.substring(0, 3));
  50.       } catch (NumberFormatException var4) {
  51.          var3 = -1;
  52.       } catch (StringIndexOutOfBoundsException var5) {
  53.          var3 = -1;
  54.       }
  55.  
  56.       if (var3 == 220) {
  57.          var2 = this.ftpin.readLine();
  58.       } else {
  59.          throw new IOException("Connect");
  60.       }
  61.    }
  62.  
  63.    public void setXferMode(int var1) {
  64.       if (var1 != this.xferMode && (var1 == 0 || var1 == 1)) {
  65.          this.xferMode = var1;
  66.          this.modeChange = true;
  67.       }
  68.  
  69.    }
  70.  
  71.    public int getXferMode() {
  72.       return this.xferMode;
  73.    }
  74.  
  75.    public void login(String var1, String var2) throws FtpLoginException {
  76.       if (var1 == null) {
  77.          throw new FtpLoginException("Login cannot be null");
  78.       } else {
  79.          try {
  80.             int var3;
  81.             if ((var3 = this.doCmd("USER " + var1 + "\n")) != 331) {
  82.                throw new FtpLoginException(this.getError(var3));
  83.             }
  84.  
  85.             if ((var3 = this.doCmd("PASS " + var2 + "\n")) != 230) {
  86.                throw new FtpLoginException(this.getError(var3));
  87.             }
  88.          } catch (IOException var5) {
  89.             throw new FtpLoginException(((Throwable)var5).getMessage());
  90.          }
  91.  
  92.          this.LoggedIn = true;
  93.       }
  94.    }
  95.  
  96.    public boolean loggedIn() {
  97.       return this.LoggedIn;
  98.    }
  99.  
  100.    public DataInputStream getFile(String var1) throws IOException {
  101.       if (this.modeChange) {
  102.          int var3;
  103.          if (this.xferMode == 0) {
  104.             var3 = this.doCmd("TYPE A\n");
  105.          } else {
  106.             var3 = this.doCmd("TYPE I\n");
  107.          }
  108.  
  109.          if (var3 != 200) {
  110.             throw new IOException("Couldn't change xfer mode");
  111.          }
  112.  
  113.          this.modeChange = false;
  114.       }
  115.  
  116.       Socket var2 = this.doIOCmd("RETR " + var1 + "\n");
  117.       String var4 = this.ftpin.readLine();
  118.       return new DataInputStream(var2.getInputStream());
  119.    }
  120.  
  121.    public Vector getFileList() throws IOException {
  122.       boolean var4 = false;
  123.       if (!this.dataRead) {
  124.          this.field_1 = new Vector(100);
  125.          this.files = new Vector(100);
  126.          this.dirs = new Vector(100);
  127.          Socket var1 = this.doIOCmd("LIST -aF\n");
  128.          DataInputStream var2 = new DataInputStream(var1.getInputStream());
  129.          var4 = false;
  130.          if (var1 == null) {
  131.             throw new IOException("Socket creation failed");
  132.          }
  133.  
  134.          while(!var4) {
  135.             String var3 = var2.readLine();
  136.             if (var3 == null) {
  137.                var4 = true;
  138.             } else {
  139.                this.field_1.addElement(var3);
  140.             }
  141.          }
  142.  
  143.          String var7 = this.ftpin.readLine();
  144.          var1.close();
  145.       }
  146.  
  147.       for(int var6 = 1; var6 < this.field_1.size(); ++var6) {
  148.          String var5 = (String)this.field_1.elementAt(var6);
  149.          if (var5.charAt(var5.length() - 1) == '/') {
  150.             this.dirs.addElement(this.getDirName((String)this.field_1.elementAt(var6)));
  151.          } else {
  152.             this.files.addElement(this.getFileName((String)this.field_1.elementAt(var6)));
  153.          }
  154.       }
  155.  
  156.       this.dataRead = true;
  157.       return this.files;
  158.    }
  159.  
  160.    public Vector getDirList() throws IOException {
  161.       if (!this.dataRead) {
  162.          Vector var1 = this.getFileList();
  163.       }
  164.  
  165.       return this.dirs;
  166.    }
  167.  
  168.    private Socket doIOCmd(String var1) throws IOException {
  169.       InetAddress var4 = InetAddress.getLocalHost();
  170.       byte[] var5 = var4.getAddress();
  171.       String var6 = "PORT ";
  172.       ServerSocket var3 = new ServerSocket(0);
  173.  
  174.       for(int var8 = 0; var8 < var5.length; ++var8) {
  175.          var6 = var6 + (var5[var8] & 255) + ",";
  176.       }
  177.  
  178.       var6 = var6 + (var3.getLocalPort() >>> 8 & 255) + "," + (var3.getLocalPort() & 255);
  179.       int var7 = this.doCmd(var6 + "\n");
  180.       if (var7 != 200 && var7 != 226 && var7 != 230) {
  181.          throw new IOException(this.getError(var7));
  182.       } else {
  183.          var7 = this.doCmd(var1);
  184.          if (var7 != 125 && var7 != 150 && var7 != 200) {
  185.             throw new IOException(this.getError(var7));
  186.          } else {
  187.             Socket var2 = var3.accept();
  188.             var3.close();
  189.             return var2;
  190.          }
  191.       }
  192.    }
  193.  
  194.    public void chdir(String var1) throws IOException {
  195.       this.dataRead = false;
  196.       int var2 = this.doCmd("CWD " + var1 + "\n");
  197.       if (var2 != 250 && var2 != 200) {
  198.          this.debug("Return code is: " + var2);
  199.          throw new IOException("Couldn't cd to: " + var1);
  200.       }
  201.    }
  202.  
  203.    private int doCmd(String var1) throws IOException {
  204.       byte[] var2 = new byte[3];
  205.       this.ftpout.print(var1);
  206.       this.ftpin.read(var2);
  207.       this.ftpin.readLine();
  208.       String var4 = new String(var2, 0);
  209.  
  210.       int var3;
  211.       try {
  212.          var3 = Integer.parseInt(var4.substring(0, 3));
  213.       } catch (NumberFormatException var5) {
  214.          var3 = -1;
  215.       } catch (StringIndexOutOfBoundsException var6) {
  216.          var3 = -1;
  217.       }
  218.  
  219.       return var3;
  220.    }
  221.  
  222.    public String getDirName(String var1) {
  223.       String var2 = null;
  224.  
  225.       for(StringTokenizer var4 = new StringTokenizer(var1); var4.hasMoreElements(); var2 = var4.nextToken()) {
  226.       }
  227.  
  228.       String var3 = var2.substring(0, var2.length() - 1);
  229.       return var3;
  230.    }
  231.  
  232.    public String getFileName(String var1) {
  233.       String var2 = null;
  234.  
  235.       for(StringTokenizer var3 = new StringTokenizer(var1); var3.hasMoreElements(); var2 = var3.nextToken()) {
  236.       }
  237.  
  238.       char var4 = var2.charAt(var2.length() - 1);
  239.       return var4 != '*' && var4 != '@' && var4 != '=' ? var2 : var2.substring(0, var2.length() - 1);
  240.    }
  241.  
  242.    public String getError(int var1) {
  243.       return this.errs.getError(var1);
  244.    }
  245.  
  246.    public void closeHard() {
  247.       try {
  248.          this.ftpin.close();
  249.          this.ftpout.close();
  250.       } catch (IOException var1) {
  251.       }
  252.  
  253.       this.LoggedIn = false;
  254.       this.dataRead = false;
  255.    }
  256.  
  257.    public void quit() throws IOException {
  258.       this.doCmd("QUIT\n");
  259.       this.ftpin.close();
  260.       this.ftpout.close();
  261.    }
  262.  
  263.    private void debug(String var1) {
  264.    }
  265. }
  266.